package com.fzy.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fzy.core.U;

public class ConvertUtils {
	private static final Log log = LogFactory.getLog(U.class);
	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
	public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat(
			DEFAULT_DATE_PATTERN);

	public static Long toLong(Object src) {
		Long result = null;
		if (src == null)
			return result;
		if (src instanceof Long) {
			result = (Long) src;
		} else if (src instanceof String) {
			try {
				result = new Long(src.toString());
			} catch (NumberFormatException e) {
				log.error("Error in converting [" + src.toString()
						+ "] to Long.", e);
			}
		}
		return result;
	}

	public static Date toDate(Object src) {
		Date result = null;
		if (src == null)
			return result;
		if (src instanceof Date) {
			result = (Date) src;
		} else if (src instanceof String) {
			String dateStr = src.toString();
			try {
				result = DEFAULT_DATE_FORMAT.parse(dateStr);
			} catch (ParseException e) {
				log.error("Error in converting [" + src.toString()
						+ "] to Date.", e);
			}
		}
		return result;
	}
}
